/**
 * 733. 图像渲染
 */
public class ImgRender {
  static int[]  dx={1,0,0,-1};
  static int[] dy={0,1,-1,0};

    public static int[][] render(int [][] src,int x,int y,int newColor){
        int currColor=src[x][y];
        if(currColor!=newColor){
            dfs(src,x,y,currColor,newColor);
        }
        return src;

    }

    public static void dfs(int[][] img,int x,int y,int color,int newColor){
        if(img[x][y]==color){
            img[x][y]=newColor;
            for (int i = 0; i < 4; i++) {
                int mx=x+dx[i],my=y+dy[i];
                if(mx>0&&mx< img.length&&my>0&&my<img[0].length){
                    dfs(img,mx,my,color,newColor);
                }
            }
        }
    }

    public static void main(String[] args) {
        int a[][]=new int[][]{{1,1,1},{1,1,0},{1,0,1}};
        render(a,1,1,2);
    }
}
